{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b3045f40",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.post(url = base_url, params = payload)\n",
    "\n",
    "r\n",
    "\n",
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a0713bdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648466221,68b9c2fd-5c7c-4152-ab68-85b64f39b68d',\n",
       " 'time_used': 600,\n",
       " 'faces': [{'face_token': '8ad8d8a456a1a824fa32de17ac2a8f2d',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bf1f5c6",
   "metadata": {},
   "source": [
    "# 人脸识别系统\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ca7eb2a",
   "metadata": {},
   "source": [
    "## 1.创建FaceSet\n",
    "* 描述\n",
    "> 1.创建一个人脸的集合FaceSet，用于存储人脸标识face_token:一个FaceSet能够存储10000个face_token\n",
    "* 调用\n",
    "> 1.https://api-cn.faceplusplus.com/facepp/v3/faceset/create\n",
    "* 调用方法\n",
    "> POST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5fda36c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c6a27ee5",
   "metadata": {},
   "outputs": [],
   "source": [
    "FaceSet_getfacesets_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "\n",
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.post(url = FaceSet_getfacesets_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1bd4e50b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 87,\n",
       " 'facesets': [{'faceset_token': '7b939caa0e516c5ca6f26cf0ee3ba05a',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '34a0056bd950948234856a353bb07e01',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648469478,ab1027df-cc23-4b30-8777-05e8a155c561'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d2b04c86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'34a0056bd950948234856a353bb07e01'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['facesets'][-1]['faceset_token']# 取值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a107278",
   "metadata": {},
   "source": [
    "## 更新数据库API\n",
    "* 描述\n",
    "> 更新一个人脸集合的属性\n",
    "\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/update\n",
    "\n",
    "* 调用方法\n",
    "> POST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "1215c460",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (Temp/ipykernel_14800/441190635.py, line 12)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"E:\\Temp/ipykernel_14800/441190635.py\"\u001b[1;36m, line \u001b[1;32m12\u001b[0m\n\u001b[1;33m    'user_data':'6层.满员.女生宿舍.460位同学.均已录入face_token'\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "FaceSet_update_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'display_name':'NFU学生宿舍',\n",
    "    'new_outer_id':'NFUSD00'\n",
    "    'user_data':'6层,满员,女生宿舍,460位同学,均已录入face_token'\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.post(url = FaceSet_update_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "aab68332",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 87,\n",
       " 'facesets': [{'faceset_token': '7b939caa0e516c5ca6f26cf0ee3ba05a',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '34a0056bd950948234856a353bb07e01',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648469478,ab1027df-cc23-4b30-8777-05e8a155c561'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f41897e1",
   "metadata": {},
   "source": [
    "# 4.检查数据集合信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "25c3d951",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "78308dab",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'faceset_token' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mE:\\Temp/ipykernel_14800/1329488872.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[1;34m'api_secret'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mAPI_secret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[1;34m'outer_id'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m'NFUSTU00'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m     \u001b[1;34m'faceset_token'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mfaceset_token\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m }\n\u001b[0;32m     12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'faceset_token' is not defined"
     ]
    }
   ],
   "source": [
    "FaceSet_getdetail_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'\n",
    "\n",
    "API_key = 'SHa1fNTCU7OPp6FMyBRYK12WzvL1isni'\n",
    "API_secret = 'Mn26GFOfOwA5LDjzgGJS-Ee9bU6Is26v'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'outer_id':'NFUSTU00',\n",
    "    'faceset_token':faceset_token\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.post(url = FaceSet_getdetail_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "42fdeb3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71bc6971",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
